#include "terrain.h"
#include <stdio.h>
#include <assert.h>
#include <malloc.h>

void terrainInit(terrain * t)
{
	int x,y;

	const char terrain_defaut[15][20] = {
		"####################",
		"#........##........#",
		"#.#####..##...####.#",
		"#........##........#",
		"#..................#",
		"#......#....#......#",
		"#......#...##......#",
		"#####..#....#..#####",
		"#......##...#......#",
		"#......#....#......#",
		"#..................#",
		"#..................#",
		"#.....#......#.....#",
		"#.....#......#.....#",
        "####################"
    };

	t->dimX = 20;
	t->dimY = 15;

	t->ter = (char **)malloc(sizeof(char *)*t->dimX);
	for (x=0; x<t->dimX; x++)
		t->ter[x] = (char *)malloc(sizeof(char)*t->dimY);

	for(x=0;x<t->dimX;x++)
    {
        for(y=0;y<t->dimY;y++)
        {
            t->ter[x][y] = terrain_defaut[y][x];
        }
    }


}



void terrainLibere(terrain *t)
{
	int y;

	for (y=0; y<t->dimY; y++)
		free(t->ter[y]);
	free(t->ter);

	t->dimX = 0;
	t->dimY = 0;
	t->ter = NULL;
}

const char getTer (const int x , const int y ,  const terrain* t)
{
	assert(x>=0);
	assert(y>=0);
	assert(x<t->dimX);
	assert(y<t->dimY);
	return t->ter[x][y];
}

int caseValable(const int x, const int y, const terrain * t)
{

	if(getTer(x,y,t)=='.')
        return 1;
	else return 0;
}

const int getDimX(const terrain* t)
{
	return t->dimX;
}

const int getDimY(const terrain* t)
{
	return t->dimY;
}

void setTer(const int x, const int y, terrain * t, const char val)
{
	assert( x>=0) ;
	assert( y>=0) ;
	assert( x<(t->dimX));
	assert( y<(t->dimY));
	t->ter[x][y] = val;
}

